import React, { JSX } from 'react';
import { NulDialog, NulCommonOperations, NulFormItem } from '../../components/NulDialog';
import '@fontsource/open-sans';
import { TextField } from '@mui/material';
import { useAppDispatch } from '../../actions/hook';
import { createNewSupplier } from '../../actions/SupplierManagement';
import '@fontsource/open-sans';

interface CreateSupplierProps {
  open: boolean;
  onClose: () => void;
}

function CreateSupplier(props: CreateSupplierProps): JSX.Element {
  const { open, onClose } = props;

  const [name, setName] = React.useState<string>('');
  const [contact, setContact] = React.useState<string>('');
  const [phone, setPhone] = React.useState<string>('');
  const [email, setEmail] = React.useState<string>('');
  const [address, setAddress] = React.useState<string>('');

  const dispatch = useAppDispatch();

  const clear = () => {
    setName('');
    setContact('');
    setPhone('');
    setEmail('');
    setAddress('');
  }

  const onCreateClicked = () => {
    dispatch(createNewSupplier({ name, contact, phone, email, address }));
    onClose();
    clear();
  };

  return (
    <NulDialog
      open={open}
      onClose={onClose}
      title='Create New Supplier'
      operations={
        <NulCommonOperations
          onPositiveClicked={onCreateClicked}
          onNegativeClicked={onClose}
          positiveText='Create'
        />
      }
    >
      <NulFormItem hint='Name' component={
        <TextField placeholder='Name' fullWidth={true} size='small' value={name} onChange={(event) => { setName(event.target.value); }} />
      } />
      <NulFormItem hint='Contact' component={
        <TextField placeholder='Contact Person Name' fullWidth={true} size='small' value={contact} onChange={(event) => { setContact(event.target.value); }} />
      } />
      <NulFormItem hint='Phone' component={
        <TextField placeholder='Phone Number' fullWidth={true} size='small' value={phone} onChange={(event) => { setPhone(event.target.value); }} />
      } />
      <NulFormItem hint='E-mail' component={
        <TextField placeholder='E-mail' fullWidth={true} size='small' value={email} onChange={(event) => { setEmail(event.target.value); }} />
      } />
      <NulFormItem hint='Address' component={
        <TextField placeholder='Address' fullWidth={true} size='small' value={address} onChange={(event) => { setAddress(event.target.value); }} />
      } />
    </NulDialog>
  );
}

export { CreateSupplier };